version 15.1
clear

* !!** define working directory **!!
cd YOURPATH/Replicationfiles/

*temp files
tempfile tf1 tf2 tf3 tf4

*Import data
*partial coefficients
import delimited "./data/partialcoeff.csv", clear 
drop v1
rename yi partialcoef
rename vi partialcoefSE
save `tf1'
*metapooled
import delimited "./data/metapooled.csv", clear 
*drop v1
rename estim metapoolcoef
rename stderr metapoolcoefSE
drop if study =="overall"
drop study
save `tf2'
import delimited "./data/pemeki_metapool.csv", clear
merge 1:1 v1 using `tf2'
keep id metapool*
save `tf2',replace


*merge
use "./data/estimates.dta",clear
keep id uid authors year_pub trust_type contxt
merge 1:1 uid using `tf1', assert(3) nogen
merge m:1 id using `tf2', nogen assert(3)

*modify author references
tempvar numaut yearstring
gen `numaut' = 1+ length(authors) - length(subinstr(authors, ",", "", .))
replace authors = subinstr(authors, ",", " &",.) if `numaut' ==2
replace authors = substr(authors, 1, strpos(authors, ",") - 1) if `numaut' >2 
replace authors = authors + " et al." if `numaut' >2 
tostring year_pub ,gen(`yearstring')
replace authors = authors + " ("+  `yearstring' + ")"
replace authors = subinstr(authors, ")", "a)",.) if id ==60
replace authors = subinstr(authors, ")", "b)",.) if id ==61

compress

*calculate means and modes
bys id: gen  numest = _N 
by id: egen avgPC = mean(partialcoef)
by id: egen avgPCSE = mean(partialcoefSE)
by id: egen trstobj_mode =  mode(trust_type) 
recode trstobj_mode .=0
label copy trust_objectLB mode_trust_objectLB 
lab def mode_trust_objectLB  0 "2+ modal values",modify
lab val trstobj_mode mode_trust_objectLB 
by id: egen contxt_mode =  mode(contxt) 
recode contxt_mode .=0
label copy contxt mode_contxtLB 
lab def mode_contxtLB   0 "2+ modal values",modify
lab val contxt_mode mode_contxtLB
rename authors Study


*aggregate to study level
egen tag = tag(id)
save `tf1', replace
keep if tag
keep id Study numest avgPC avgPCSE trstobj_mode contxt_mode metapoolcoef metapoolcoefSE
foreach var of varlist avg* metapool* {
replace `var' = `var'*100
}
compress

tab1 trstobj_mode contxt_mode 


*gen Table D1
sort Study 
capture putdocx clear
*Tab1
putdocx begin, pagesize(A4) font("Ariel", 11) landscape
putdocx paragraph, 
putdocx text ("Table D1")
putdocx table TabDes1 = data(Study numest metapoolcoef metapoolcoefSE avgPC  trstobj_mode contxt_mode) ///
 , varnames border(start, nil) border(insideH, nil) border(insideV, nil) border(end, nil) headerrow(1) layout(autofitcontents) ///
 cellmargin(left , 0) cellmargin(right , 0.1) cellmargin(bottom , 0.1)
putdocx table TabDes1(1,2) = ("Number of estimates used")
putdocx table TabDes1(1,3) = ("Study-Pooled Estimate (x100)")
putdocx table TabDes1(1,4) = ("SE of Study-Pooled Est. (x100)")
putdocx table TabDes1(1,5) = ("Avg. Partial Coefficient (x100)")
putdocx table TabDes1(1,6) = ("Modal Type of Trust")
putdocx table TabDes1(1,7) = ("Modal Context Type")
putdocx table TabDes1(.,2/5),halign(center)
putdocx table TabDes1(1,.),border(bottom,single)
putdocx table TabDes1(.,2),nformat(%4.0f)
putdocx table TabDes1(.,3/5),nformat(%4.2f)
putdocx table TabDes1(45,.),nosplit

putdocx save "./tables/D1_D3", replace

*Table 2
use `tf1',clear
*trust obj
tempvar tv1 tv2
loc label1
loc numstu
loc numest
    levelsof trust_type, local(levels)  
    foreach l of local levels {
	loc label1 `label1' `l'
    egen `tv1' = tag(id) if trust_type  ==`l'
	egen `tv2' = total(`tv1')
	sca tv3 = `tv2'
	loc tv3 =  tv3
	loc numstu `numstu' `tv3' 
	drop `tv1' `tv2'
	count if trust_type  ==`l'
	loc numest `numest' `r(N)'
	}
	
mat input  A = (`numstu')
mat input B = (`label1')
mat input C = (`numest')
mat list C
mat AA = B',C',A'
mat list AA

*context
tempvar tv1 tv2
loc label1
loc numstu
loc numest
    levelsof contxt, local(levels)  
    foreach l of local levels {
	loc label1 `label1' `l'
    egen `tv1' = tag(id) if contxt  ==`l'
	egen `tv2' = total(`tv1')
	sca tv3 = `tv2'
	loc tv3 =  tv3
	loc numstu `numstu' `tv3' 
	drop `tv1' `tv2'
	count if contxt ==`l'
	loc numest `numest' `r(N)'
	}
	
mat input  A = (`numstu')
mat input B = (`label1')
mat input C = (`numest')
mat list C
mat BB = B',C',A'
mat list BB

svmat AA
keep AA*
drop if AA1==.
lab val AA1 trust_objectLB
rename AA2 numcoefs
rename AA3 numstud
decode AA1, gen(Moderator)
drop AA1
gen sort = _n
count
loc rep = `r(N)'+1
set obs `rep'
replace Moderator = "TO" in `rep'
replace sort = 0 in `rep'
sort sort
save `tf2',replace

svmat BB
keep BB*
drop if BB1==.
lab val BB1 contxt
rename BB2 numcoefs
rename BB3 numstud
decode BB1, gen(Moderator)
drop BB1
gen sort = _n
count
loc rep = `r(N)'+1
set obs `rep'
replace Moderator = "CO" in `rep'
replace sort = 0 in `rep'
sort sort
save `tf3',replace


use `tf2'
append using `tf3'
drop sort
gen sort = _n
order Moderator
save `tf2',replace


import delimited "./data/TrustObject.csv", clear
rename v1 Moderator
rename estimate Prediction
rename stderror SEofPred
drop in 2
replace Moderator = "Generalized other" in 1
replace Moderator = "Neighbors/people in local area" in 2
replace Moderator = "Ethnic/religious out-group" in 3
replace Moderator = "Ethnic/religious in-group" in 4
replace Moderator = "Other" in 5
drop study which
compress
save `tf3',replace


import delimited "./data/ContextSize.csv", clear
drop v1
rename estimate Prediction
rename stderror SEofPred
drop in 2
rename study Moderator
drop which
compress
save `tf4',replace

use `tf2'

merge 1:1 Moderator using `tf3', assert(1 3) nogen
merge 1:1 Moderator using `tf4',  nogen update

sort sort
drop sort
replace Prediction = Prediction*100
replace SEofPred = SEofPred *100



*Table D2
capture putdocx clear
putdocx begin, pagesize(A4) font("Ariel", 12) landscape
putdocx pagebreak
putdocx paragraph, 
putdocx text ("Table D2")
putdocx table TabDes2 = data(*) ///
 , varnames border(start, nil) border(insideH, nil) border(insideV, nil) border(end, nil) headerrow(1) layout(autofitcontents) ///
 cellmargin(left , 0) cellmargin(right , 0.2) cellmargin(bottom , 0.1)
putdocx table TabDes2(1,2) = ("# estimates")
putdocx table TabDes2(1,3) = ("# studies")
putdocx table TabDes2(1,4) = ("Prediction (x100)")
putdocx table TabDes2(1,5) = ("SE of prediction (x100)")
putdocx table TabDes2(.,2/5),halign(center)
putdocx table TabDes2(1,.),border(bottom,single)
putdocx table TabDes2(.,2/3),nformat(%4.0f)
putdocx table TabDes2(.,4/5),nformat(%4.2f)
putdocx table TabDes2(2,2) = ("TRUST TYPE")
putdocx table TabDes2(2,3) = ("")
putdocx table TabDes2(2,4) = ("")
putdocx table TabDes2(2,5) = ("")
putdocx table TabDes2(8,1) = ("CONTEXT TYPE")
putdocx table TabDes2(8,2) = ("")
putdocx table TabDes2(8,3) = ("")
putdocx table TabDes2(8,4) = ("")
putdocx table TabDes2(8,5) = ("")
putdocx save "./tables/D1_D3", append




*table D3 + intro
capture putdocx clear
putdocx begin, pagesize(A4) font("Ariel", 12) landscape
putdocx pagebreak
putdocx paragraph, 
putdocx text ("Table D3")
use "./data/estimates.dta",clear
keep id multi_div contr_crime contr_inteth_contact contr_cc_ses contr_ci_ses contr_minority 
mean contr_inteth_contact contr_cc_ses contr_ci_ses contr_crime contr_minority multi_div
putdocx table d3 = etable, title("")
putdocx save "./tables/D1_D3", append
exit
